﻿namespace Smart.Utils.UI.BarcodeLib
{
    using System;

    internal class EAN8 : BarcodeCommon, IBarcode
    {
        private string[] EAN_CodeA = new string[] { "0001101", "0011001", "0010011", "0111101", "0100011", "0110001", "0101111", "0111011", "0110111", "0001011" };
        private string[] EAN_CodeC = new string[] { "1110010", "1100110", "1101100", "1000010", "1011100", "1001110", "1010000", "1000100", "1001000", "1110100" };

        public EAN8(string input)
        {
            base.Raw_Data = input;
            this.CheckDigit();
        }

        private void CheckDigit()
        {
            if (base.Raw_Data.Length == 7)
            {
                int num3;
                int num = 0;
                int num2 = 0;
                for (num3 = 0; num3 <= 6; num3 += 2)
                {
                    num2 += int.Parse(base.Raw_Data.Substring(num3, 1)) * 3;
                }
                for (num3 = 1; num3 <= 5; num3 += 2)
                {
                    num += int.Parse(base.Raw_Data.Substring(num3, 1));
                }
                int num4 = num + num2;
                int num5 = num4 % 10;
                num5 = 10 - num5;
                if (num5 == 10)
                {
                    num5 = 0;
                }
                base.Raw_Data = base.Raw_Data + num5.ToString();
            }
        }

        private string Encode_EAN8()
        {
            int num;
            char ch;
            if ((base.Raw_Data.Length != 8) && (base.Raw_Data.Length != 7))
            {
                throw new Exception("EEAN8-1: Invalid data length. (7 or 8 numbers only)");
            }
            if (!BarcodeEdit.CheckNumericOnly(base.Raw_Data))
            {
                throw new Exception("EEAN8-2: Numeric only.");
            }
            string str = "101";
            for (num = 0; num < (base.Raw_Data.Length / 2); num++)
            {
                ch = base.Raw_Data[num];
                str = str + this.EAN_CodeA[int.Parse(ch.ToString())];
            }
            str = str + "01010";
            for (num = base.Raw_Data.Length / 2; num < base.Raw_Data.Length; num++)
            {
                ch = base.Raw_Data[num];
                str = str + this.EAN_CodeC[int.Parse(ch.ToString())];
            }
            return (str + "101");
        }

        public string Encoded_Value
        {
            get
            {
                return this.Encode_EAN8();
            }
        }
    }
}

